package com.rxst.msg.webSocket;

import com.alibaba.fastjson.JSON;
import com.rxst.msg.webSocket.model.SocketConnectModel;
import com.rxst.msg.webSocket.utils.RedisUtil;
import com.rxst.msg.webSocket.utils.SocketServerUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.net.InetSocketAddress;
import java.util.*;
@Slf4j
public class MyWebSocketService extends WebSocketServer {
    RedisUtil redisUtil = new RedisUtil();
    Map<String,String>userMap = new HashMap<>();
    public MyWebSocketService(int port) {
        super(new InetSocketAddress(port));
        redisUtil.delete("plugFlowList");
        System.out.println("启动websocket");
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        log.info(conn.toString()+"已连接");
    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        log.info("连接关闭 "+conn.toString());
        try {
            String userName = userMap.get(conn.toString());
            Object plugFlowList = redisUtil.hget("plugFlowList",userName);
            if (plugFlowList!=null) {
                redisUtil.hdel("plugFlowList", userName);
                log.info("redis缓存数据为 "+redisUtil.hget("plugFlowList"));
            }
        } catch (Exception e) {
            System.err.println(e);
        }
    }

    @Override
    public void onMessage(WebSocket socket, String message) {
        Map<String, List<WebSocket>> listMap = SocketServerUtils.listMap;
        log.info(socket.toString()+"发送消息 ");
        log.info("收到消息 "+message);
        try {
            SocketConnectModel socketConnectModel = JSON.parseObject(message, SocketConnectModel.class);
            if (socketConnectModel.getType().equals("200")) {
                //推流地址
                String address = socketConnectModel.getData().getAddress();
                String substring = address.substring(address.lastIndexOf("rtmp")).replace("rtmp", "http").replace("/live", ":7777/live");
                //登录用户名
                String userName = socketConnectModel.getData().getUserName();
                if (StringUtils.isBlank(userName)){
                    userName = socket.toString();
                }
                userMap.put(socket.toString(),userName);
                //ip地址
                String ip = socketConnectModel.getData().getIp();
                Map<String, Object> map = new HashMap<>();
                map.put("userName", userName);
                map.put("address", substring);
                try {
                    redisUtil.hset("plugFlowList", userName, map.toString());
                    log.info("redis缓存数据为 "+redisUtil.hget("plugFlowList", socket.toString()));
                } catch (Exception e) {
                    System.err.println(e);
                }
            }
        }catch (Exception e){
            System.err.println(e);
        }
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {
        log.info("发生错误"+conn.toString());
        log.info("发生错误,错误为: "+ex);
        try {
            String userName = userMap.get(conn.toString());
            Object plugFlowList = redisUtil.hget("plugFlowList", userName);
            if (plugFlowList!=null) {
                redisUtil.hdel("plugFlowList", userName);
                log.info("redis缓存数据为 "+redisUtil.hget("plugFlowList"));
            }
        } catch (Exception e) {
            System.err.println(e);
        }
    }

    @Override
    public void onStart() {

    }

}
